package com.rpframework.website.device.act.api;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonPrimitive;
import com.rpframework.core.BaseAct;
import com.rpframework.core.utils.DictionarySettingUtils;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Category;
import com.rpframework.website.device.domain.GoodsPro;
import com.rpframework.website.device.domain.GoodsProStockRecord;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.exception.APICodeException;
import com.rpframework.website.device.service.CategoryService;
import com.rpframework.website.device.service.GoodsProService;
import com.rpframework.website.device.service.GoodsProStockRecordService;
import com.rpframework.website.device.service.UserService;

/**
 * 商品模块接口
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/api/goodspro")
public class GoodsProApiAct extends BaseAct {
	@Resource GoodsProService service;
	@Resource CategoryService serviceCategory;
	@Resource UserService userService;
	@Resource GoodsProStockRecordService recordService;

	@RequestMapping(value="/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String listone(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="category",required = false ) String category,/*1.冷鲜食品  2.休闲食品3.奶酒饮料4.粮油副食5.清洁用品6.个人护理7.生活日用8.母婴用品9.其它*/
			@RequestParam(value="label",required = false ) String label,/*标签 1.热门推荐 2.精品推荐 3.促销专区 4.热卖榜单*/  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="brand",required = false ) String brand,/*品牌*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		
		
		
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		if(StringUtils.isNotBlank(category))pager.getSearchMap().put("category", category);
		if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(StringUtils.isNotBlank(brand))pager.getSearchMap().put("brand", brand);
		pager = service.getPager(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("id", t.getId());	
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.add("urlList", arr);//商品图片
				obj.addProperty("name", t.getName());//商品名字
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
		return   ""+json  ;
	}
	
	
	
	@RequestMapping(value="/user/list",produces = "application/json; charset=utf-8")
	public @ResponseBody String listoneWithSum(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="pageSize",required = false ) Integer pageSize,
			@RequestParam(value="category",required = false ) String category,/*1.冷鲜食品  2.休闲食品3.奶酒饮料4.粮油副食5.清洁用品6.个人护理7.生活日用8.母婴用品9.其它*/
			@RequestParam(value="label",required = false ) String label,/*标签 1.热门推荐 2.精品推荐 3.促销专区 4.热卖榜单*/  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="brand",required = false ) String brand,/*品牌*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		
		if(pageSize!=null)pager.setPageSize(pageSize);
		
		Integer userId=user.getId();
		if(StringUtils.isNotBlank(userId.toString()))pager.getSearchMap().put("userId", userId.toString());
		if(StringUtils.isNotBlank(category))pager.getSearchMap().put("category", category);
		if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(StringUtils.isNotBlank(brand))pager.getSearchMap().put("brand", brand);
		
		pager = service.getPagerStock(pager);
		JsonObject json=new JsonObject();
		
		
		
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				
				
				
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.addProperty("id", t.getId());//产品Id
				
				
				JsonArray a = new JsonArray();
				for(JsonElement li : arr){
					a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
				}
				obj.add("urlList", a);
				
				
				
//				obj.add("urlList", arr);//商品图片
				obj.addProperty("name", t.getName());//产品品名字
				obj.addProperty("goodsModel", t.getGoodsModel());//产品型号
				obj.addProperty("brand", t.getBrand());//产品品牌
				obj.addProperty("goodsCode", t.getGoodsCode());//产品品牌
				
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				obj.addProperty("stock", t.getStock());//产品库存
				array.add(obj);
				
			}
			json.add("arrays", array);
		}
		if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
		return   ""+json  ;
	}
	
							
	
	@RequestMapping(value="/listgoodsbrand",produces = "application/json; charset=utf-8")
	public @ResponseBody String listBrandByPlatform(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			
			@RequestParam(value="label",required = false ) String label,/*标签 */  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		
		
		if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		
		pager = service.getPagerBrandByPlatform(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				
				obj.addProperty("brand", t.getBrand());//商品品牌
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	@RequestMapping(value="/user/listgoodsbrand",produces = "application/json; charset=utf-8")
	public @ResponseBody String listBrandByUser(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="userId",required = false ) String userId,/*标签 */
			@RequestParam(value="label",required = false ) String label,/*标签 */  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		if(StringUtils.isNotBlank(user.getId().toString()))pager.getSearchMap().put("userId", user.getId().toString());
	    if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		
		pager = service.getPagerBrandByUser(pager);
		
		
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				
				obj.addProperty("brand", t.getBrand());//商品品牌
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	
	
	
	
	
	@RequestMapping(value="/user/listgoodsname",produces = "application/json; charset=utf-8")
	public @ResponseBody String listName(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="userId",required = false ) String userId,/*标签 */
			@RequestParam(value="label",required = false ) String label,/*标签 */  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		if(StringUtils.isNotBlank(user.getId().toString()))pager.getSearchMap().put("userId", user.getId().toString());
	    if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		
		pager = service.getPagerName(pager);
		
		
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				
				obj.addProperty("goodsName", t.getName());//商品型号
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	
	
	@RequestMapping(value="/user/listgoodsmodel",produces = "application/json; charset=utf-8")
	public @ResponseBody String listModel(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="userId",required = false ) String userId,/*标签 */
			@RequestParam(value="label",required = false ) String label,/*标签 */  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="goodsName",required = true ) String goodsName,/*用户选择的产品名*/
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		if(StringUtils.isNotBlank(user.getId().toString()))pager.getSearchMap().put("userId", user.getId().toString());
	    if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(StringUtils.isNotBlank(goodsName))pager.getSearchMap().put("goodsName", goodsName);
		pager = service.getPagerModel(pager);
		
		
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				
				obj.addProperty("goodsModel", t.getGoodsModel());//商品型号
				
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	@RequestMapping(value="/listgoodsbycategory",produces = "application/json; charset=utf-8")
	public @ResponseBody String listGoodsWithCategory(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			
			@RequestParam(value="categoryId",required = true ) String categoryId,/*类别id*/
			@RequestParam(value="label",required = false ) String label,/*标签 1.热门推荐 2.精品推荐 3.促销专区 4.热卖榜单*/  
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="pageSize",required = false ) Integer pageSize,
			
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		
		if(StringUtils.isNotBlank(categoryId))pager.getSearchMap().put("categoryId", categoryId);
		if(StringUtils.isNotBlank(label))pager.getSearchMap().put("label", label);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(pageSize!=null)pager.setPageSize(pageSize);
		
		pager = service.getPagerByCategory(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("Id", t.getId());//产品Id
				obj.addProperty("name", t.getName());//产品名
				obj.addProperty("goodsModel", t.getGoodsModel());//产品型号
				obj.addProperty("brand", t.getBrand());//商品品牌
				obj.addProperty("goodsCode", t.getGoodsCode());//产品代码
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				
				
				JsonArray a = new JsonArray();
				for(JsonElement li : arr){
					a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
				}
				obj.add("urlList", a);
				
				
				//obj.add("urlList", arr);//商品图片
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	
	
	
	@RequestMapping(value="/listgoodsbybrand",produces = "application/json; charset=utf-8")
	public @ResponseBody String listGoodsWithBrand(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="pageSize",required = false ) Integer pageSize,
			
			@RequestParam(value="brand",required = true ) String brand,/*品牌数据字典id*/
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		if(pageSize!=null)pager.setPageSize(pageSize);
		if(StringUtils.isNotBlank(brand))pager.getSearchMap().put("brand", brand);
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		
		pager = service.getPagerByBrand(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("Id", t.getId());//产品Id
				obj.addProperty("name", t.getName());//产品名
				obj.addProperty("goodsModel", t.getGoodsModel());//产品型号
				obj.addProperty("brand", t.getBrand());//商品品牌
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				obj.addProperty("goodsCode", t.getGoodsCode());//产品代码
				
				
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.add("urlList", arr);//商品图片
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	
	
	
	
	
	
	/**
	 * 商品详情 
	 * @param id
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/detail",produces = "application/json; charset=utf-8")
	public @ResponseBody String Detail(@RequestParam(value="id",required = true ) Integer id,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		
		GoodsPro t = service.select(id);
		Category c=serviceCategory.select(t.getCategoryId());
		
		JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();

		JsonArray a = new JsonArray();
		for(JsonElement li : arr){
			a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
		}
		json.add("urlList", a);
		
		
		
		
//		json.add("urlList", arr);
//		
//		JsonArray arrDs = new JsonParser().parse(t.getUrlListDs()).getAsJsonArray();
//		
//		JsonArray b = new JsonArray();
//		for(JsonElement li : arrDs){
//			a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
//		}
//		json.add("urlListDs", b);
		
		
//		json.add("urlListDs", arrDs);
		
		json.addProperty("name", t.getName());
		json.addProperty("alias", t.getAlias());
		json.addProperty("goodsModel", t.getGoodsModel());
		json.addProperty("goodsCode", t.getGoodsCode());
		json.addProperty("brand", t.getBrand());
		
		String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
		json.addProperty("brandName", brandName);
		
		
		json.addProperty("category", c.getName());
		json.addProperty("baseStationModel", t.getBaseStationModel());
		json.addProperty("net", t.getNet());
		
		json.addProperty("computerCardName", t.getComputerCardName());
		json.addProperty("production", t.getProduction());
		
		
		if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
		return   ""+json  ;
	}
	
	
	
	/**
	 * 商品详情 
	 * @param id
	 * @param callback
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/user/detail",produces = "application/json; charset=utf-8")
	public @ResponseBody String myDetail(@RequestParam(value="id",required = true ) Integer id,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		JsonObject json = new JsonObject();
		User user = getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "您尚未登录!");
		}
		
		GoodsPro t = service.getOneStock(user.getId(), id);
		if(t!=null){

			JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
			
			JsonArray a = new JsonArray();
			for(JsonElement li : arr){
				a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
			}
			json.add("urlList", a);
			
//			json.add("urlList", arr);
			
			
//			JsonArray arrDs = new JsonParser().parse(t.getUrlListDs()).getAsJsonArray();
//			
//			JsonArray b = new JsonArray();
//			for(JsonElement li : arrDs){
//				a.add(new JsonPrimitive(TagUtils.getFileFullPath(li.toString().replace("\"", ""))));
//			}
//			json.add("urlListDs", b);
			
//			json.add("urlListDs", arrDs);
			
			json.addProperty("name", t.getName());
			json.addProperty("alias", t.getAlias());
			json.addProperty("goodsModel", t.getGoodsModel());
			json.addProperty("goodsCode", t.getGoodsCode());
			json.addProperty("brand", t.getBrand());
			
			String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
			json.addProperty("brandName", brandName);

			json.addProperty("baseStationModel", t.getBaseStationModel());
			json.addProperty("net", t.getNet());
			json.addProperty("computerCardName", t.getComputerCardName());
			json.addProperty("production", t.getProduction());
			json.addProperty("stock", t.getStock());
			
			
		}

		if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
		return   ""+json  ;
	}
	
	
	@RequestMapping(value="/user/newgoods",produces = "application/json; charset=utf-8")
	public @ResponseBody String CreateByUser(
			@RequestParam(value = "name",required = true ) String name,
			@RequestParam(value = "alias",required = false ) String alias,
			@RequestParam(value = "goodsModel",required = true ) String goodsModel,
			@RequestParam(value = "goodsCode",required = false ) String goodsCode,
			@RequestParam(value = "brand",required = true ) Integer brand,
			@RequestParam(value = "categoryId",required = true ) Integer categoryId,
			@RequestParam(value = "baseStationModel",required = false ) String baseStationModel,
			@RequestParam(value = "net",required = false ) String net,
			@RequestParam(value = "computerCardName",required = false ) String computerCardName,
			@RequestParam(value = "production",required = false ) String production,
			@RequestParam(value = "urlList",required = true ) String urlList,
			@RequestParam(value = "amount",required = true ) Integer amount,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session
			){
		JsonObject json = new JsonObject();
		User user=getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		
		GoodsPro r=new GoodsPro();
		r.setName(name);
		r.setAlias(alias);
		r.setGoodsModel(goodsModel);
		r.setGoodsCode(goodsCode);
		r.setBrand(brand);
		r.setCategoryId(categoryId);
		r.setBaseStationModel(baseStationModel);
		r.setNet(net);
		r.setComputerCardName(computerCardName);
		r.setProduction(production);
		r.setUrlList(urlList);
		r.setCreateTime(System.currentTimeMillis()/1000);
		
		r.setBuilderId(user.getId());
		
		
		
		
		
		boolean flag = service.insert(r);
		if(flag){
			json.addProperty("code", 200);
			json.addProperty("msg", "发布成功！");
			
			
			GoodsProStockRecord gr=new GoodsProStockRecord();
			Integer builderId=user.getId();
			List<GoodsPro> listGoods=service.getGoodsBuilderIdandName(name,builderId);
			Integer goodsProId=listGoods.get(0).getId();
			gr.setUserId(user.getId());
			gr.setGoodsProId(goodsProId);
			gr.setAmount(amount);
			gr.setCreateTime(System.currentTimeMillis()/1000);
			recordService.insert(gr);
			
		}else{
			json.addProperty("code", 500);
			json.addProperty("msg", "发布失败！");
		}
		if(StringUtils.isNotBlank(callback))return (callback + "(" + json + ")");
		return ""+json ;
		
	}
	
	
	
	
	
	
	@RequestMapping(value="/listgoodsh",produces = "application/json; charset=utf-8")
	public @ResponseBody String listGoodsSearch(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="search",required = true ) String search,/*搜索词*/
			@RequestParam(value="pageSize",required = false ) Integer pageSize,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		
		if(pageSize!=null)pager.setPageSize(pageSize);
		
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("name", search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("goodsModel",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("goodsCode", search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("baseStationModel",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("computerCardName",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("production",  search);
		
		pager = service.getPagerSearch(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("Id", t.getId());//产品Id
				obj.addProperty("name", t.getName());//产品名
				obj.addProperty("goodsModel", t.getGoodsModel());//0产品型号
				obj.addProperty("brand", t.getBrand());//商品品牌
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				obj.addProperty("goodsCode", t.getGoodsCode());//产品代码
				
				
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.add("urlList", arr);//商品图片
				array.add(obj);
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	@RequestMapping(value="/user/listgoodsh",produces = "application/json; charset=utf-8")
	public @ResponseBody String listGoodsSearchByUser(@RequestParam(value="pager",required = false ) Pager<GoodsPro> pager,
			
			
			
			@RequestParam(value="state",required = false ) String state,/*状态 1.正常 2.禁用*/
			@RequestParam(value="search",required = true ) String search,/*搜索词*/
			@RequestParam(value="pageSize",required = false ) Integer pageSize,
			@RequestParam(value = "callback",required = false ) String callback,
			HttpSession session){
		if(pager==null){
			pager=new Pager<GoodsPro>();
		}
		
		if(pageSize!=null)pager.setPageSize(pageSize);
		
		User user=getSessionUser(session);
		if(user==null){
			throw new APICodeException(-4, "你还没登陆!");
		}
		
		String userId=user.getId().toString();
		
		if(StringUtils.isNotBlank(userId))pager.getSearchMap().put("userId", userId);
		
		if(StringUtils.isNotBlank(state))pager.getSearchMap().put("state", state);
		
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("name", search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("goodsModel",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("goodsCode", search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("baseStationModel",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("computerCardName",  search);
		if(StringUtils.isNotBlank(search))pager.getSearchMap().put("production",  search);
		
		pager = service.getPagerSearchByUser(pager);
		JsonObject json=new JsonObject();
		json.addProperty("totalPages", pager.getTotalPages());
		json.addProperty("currentPage", pager.getCurrentPage());
		json.addProperty("totalCount", pager.getTotalCount());
		List<GoodsPro> list=pager.getItemList();
		if(list!=null && list.size()>0){
		JsonArray array=new JsonArray();
			for(GoodsPro t : list){
				JsonObject obj = new JsonObject();
				obj.addProperty("Id", t.getId());//产品Id
				obj.addProperty("name", t.getName());//产品名
				obj.addProperty("goodsModel", t.getGoodsModel());//0产品型号
				obj.addProperty("brand", t.getBrand());//商品品牌
				
				String brandName=DictionarySettingUtils.getParameterValue("goods.brand."+t.getBrand());
				obj.addProperty("brandName", brandName);
				
				obj.addProperty("goodsCode", t.getGoodsCode());//产品代码
				
				
				JsonArray arr = new JsonParser().parse(t.getUrlList()).getAsJsonArray();
				obj.add("urlList", arr);//商品图片
				
				obj.addProperty("stock", t.getStock());
				
				array.add(obj);
				
			}
			json.add("arrays", array);
		}
	if(StringUtils.isNotBlank(callback))	return (callback + "(" + json + ")");
	return   ""+json  ;
	}
	
	
	
	
	
	
	
}
